//支付成功页
const app = getApp();
var server = require('../../utils/server');
var common = require('../../utils/common');
Page({
  data: {
    recommendGoods: [],
    orderId: ""
  },
  onLoad: function (options) {
    this.setData({
      orderId: options.orderId
    })
  },
  onShow: function () {
    wx.getStorage({
      key: 'storeId',
      success: res => {
        this.data.storeId = res.data;
        this.getRecommendGoods();
      },
    })
  },

  getRecommendGoods() {
    wx.getStorage({
      key: 'recommendIds',
      success: resp => {
        server.postJSON(common.storePath + 'store/getrecommendgoods/', {
          "store_id": this.data.storeId,
          "recommend_type": 'like',
          "recommend_num": 4,
          "goods_ids": resp.data
        }, res => {
          if (res.data.errno == '0') {
            if (res.data.data.length > 0) {
              (res.data.data).forEach((item, index) => {
                item.isCanAdd = true;
                item.isCanReduce = true;
              })
              this.setData({
                recommendGoods: res.data.data
              })
              if (common.compareVersion(wx.getSystemInfoSync().SDKVersion, '1.9.6') >= 0) {
                this.data.recommendGoods.forEach((item, idx) => {
                  let intersect = wx.createIntersectionObserver();
                  intersect.relativeToViewport().observe('#easy-rec-' + item.goods_id, (res) => {
                    intersect.disconnect(); //使用disconnect，使得目标节点只曝光一次
                    console.log('曝光#easy-rec-' + item.goods_id);
                    server.postJSON(common.storePath + 'store/reportusertrace/', {
                      "store_id": this.data.storeId,
                      "recommend_type": '猜你喜欢',
                      "list": [{
                        "goods_id": item.goods_id,
                        "trace_id": item.trace_id,
                        "position": idx
                      }]
                    }, res => {
                      if (res.data.errno == '0') {
                        console.log('曝光成功')
                      } else {
                        common.showErrorToast(res.data.errmsg)
                      }
                    })
                  })
                })
              }
            }
          } else {
            common.showErrorToast(res.data.errmsg)
          }
        })
      }
    })

  },
  //添加为你推荐商品
  addRecommendCart(e) {
    var index = e.currentTarget.dataset.index;
    this.setData({
      ['recommendGoods[' + index + '].isCanAdd']: false
    })
    server.postJSON(common.storePath + 'cart/addgoods/', {
      "store_id": this.data.storeId,
      "goods_id": e.currentTarget.dataset.id,
      "trace_id": e.currentTarget.dataset.traceid
    }, res => {
      if (res.data.errno == "0") {
        common.showErrorToast("添加成功");
        this.setData({
          cartNum: parseInt(this.data.cartNum) + 1,
          ['recommendGoods[' + index + '].isCanAdd']: true,
          ['recommendGoods[' + index + '].cart_num']: parseInt(this.data.recommendGoods[index].cart_num) + 1
        })
      } else {
        this.setData({
          ['recommendGoods[' + index + '].isCanAdd']: true
        })
        common.showErrorToast(res.data.errmsg);
      }
    })
  },
  addEmpty() {
    console.log('waiting add success')
  },
  goToOrderDetail() {
    wx.reLaunch({
      url: '/order/pages/orderDetail?orderId=' + this.data.orderId
    })
  },
  goToIndex() {
    wx.switchTab({
      url: '../../pages/index/index',
    })
    app.globalData.isChangeIndexLocation = true;
  }
})